package com.wpp.mq.rabbitmqsqmple.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.wpp.mq.rabbitmqsqmple.factory.MyConnectionFactoryBuilder;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @description: direct 生产者
 * @author: WangPengPeng
 * @create: 2021-11-28 21:48
 **/
public class EmitLogsDirect {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = MyConnectionFactoryBuilder.buildConnection();

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String levelLogRouteKey = null;

        // 声明direct类型的交换器logs
        channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        for (int i = 0; i < 100; i++) {
            switch (i % 3) {
                case 0:
                    levelLogRouteKey = "info";
                    break;
                case 1:
                    levelLogRouteKey = "warn";
                    break;
                case 2:
                    levelLogRouteKey = "error";
                    break;
                default:
                    System.err.println("log错误，程序退出");
                    System.exit(-1);
            }
            String logStr = "这是 【" + levelLogRouteKey + "】 的消息";
            channel.basicPublish("direct_logs", levelLogRouteKey, null, logStr.getBytes("UTF-8"));
        }
    }
}
